#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 取消窗口的最大化按钮
    this->setWindowFlags(this->windowFlags() & ~Qt::WindowMaximizeButtonHint);

    // 获取SC管理器
    this->sc_httpd = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
    this->sc_mysqld = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
    if (this->sc_httpd == NULL || this->sc_mysqld == NULL) {
        QMessageBox::critical(this, "System error", "Can't get SC manager!");
        return;
    }

    // 获取Apache与MySQL的服务状态
    this->sc_httpd_service = OpenService(this->sc_httpd, L"Apache2.4", SERVICE_ALL_ACCESS);
    this->sc_mysqld_service = OpenService(this->sc_mysqld, L"MySQL", SERVICE_ALL_ACCESS);

    if (this->sc_httpd_service == NULL) {
        CloseServiceHandle(this->sc_httpd);
    }
    if (this->sc_mysqld_service == NULL) {
        CloseServiceHandle(this->sc_mysqld);
    }
    if (this->sc_httpd_service == NULL || this->sc_mysqld_service == NULL) {
        QMessageBox::critical(this, "System error", "Can't open service: " + QVariant((qulonglong)GetLastError()).toString());
        return;
    }

    // 获取服务状态
    this->refresh_status();
}

MainWindow::~MainWindow()
{
    CloseServiceHandle(this->sc_httpd);
    CloseServiceHandle(this->sc_httpd_service);
    CloseServiceHandle(this->sc_mysqld);
    CloseServiceHandle(this->sc_mysqld_service);
    delete ui;
}

void MainWindow::refresh_status() {
    // 显示Apache与MySQL的服务状态
    if (!QueryServiceStatus(this->sc_httpd_service, &this->http_status)) {
        QMessageBox::critical(this, "系统错误", "无法查询HTTP状态：" + QVariant((qulonglong)GetLastError()).toString());
        return;
    }
    if (!QueryServiceStatus(this->sc_mysqld_service, &this->mysql_status)) {
        QMessageBox::critical(this, "系统错误", "无法查询MySQL状态：" + QVariant((qulonglong)GetLastError()).toString());
        return;
    }

    switch (this->http_status.dwCurrentState) {
    case SERVICE_CONTINUE_PENDING:
        this->internal_http_status = APACHE_RUNNING;
        this->ui->apache_start_or_stop_button->setEnabled(false);
//        this->ui->apache_restart_button->setEnabled(false);
        break;
    case SERVICE_PAUSE_PENDING:
        this->internal_http_status = APACHE_PAUSED;
        this->ui->apache_start_or_stop_button->setEnabled(false);
//        this->ui->apache_restart_button->setEnabled(false);
        break;
    case SERVICE_PAUSED:
        this->internal_http_status = APACHE_PAUSED;
        this->ui->apache_start_or_stop_button->setEnabled(true);
//        this->ui->apache_restart_button->setEnabled(false);
        this->ui->apache_start_or_stop_button->setText("继续");
        break;
    case SERVICE_RUNNING:
        this->internal_http_status = APACHE_RUNNING;
        this->ui->apache_start_or_stop_button->setEnabled(true);
//        this->ui->apache_restart_button->setEnabled(true);
        this->ui->apache_start_or_stop_button->setText("关闭");
        break;
    case SERVICE_START_PENDING:
        this->internal_http_status = APACHE_RUNNING;
        this->ui->apache_start_or_stop_button->setEnabled(false);
//        this->ui->apache_restart_button->setEnabled(false);
        break;
    case SERVICE_STOP_PENDING:
        this->internal_http_status = APACHE_STOPPED;
        this->ui->apache_start_or_stop_button->setEnabled(false);
//        this->ui->apache_restart_button->setEnabled(false);
        break;
    case SERVICE_STOPPED:
        this->internal_http_status = APACHE_STOPPED;
        this->ui->apache_start_or_stop_button->setEnabled(true);
//        this->ui->apache_restart_button->setEnabled(false);
        this->ui->apache_start_or_stop_button->setText("开启");
        break;
    }

    switch (this->mysql_status.dwCurrentState) {
    case SERVICE_CONTINUE_PENDING:
        this->internal_mysql_status = MYSQL_RUNNING;
        this->ui->mysql_start_or_stop_button->setEnabled(false);
//        this->ui->mysql_restart_button->setEnabled(false);
        break;
    case SERVICE_PAUSE_PENDING:
        this->internal_mysql_status = MYSQL_PAUSED;
        this->ui->mysql_start_or_stop_button->setEnabled(false);
//        this->ui->mysql_restart_button->setEnabled(false);
        break;
    case SERVICE_PAUSED:
        this->internal_mysql_status = MYSQL_PAUSED;
        this->ui->mysql_start_or_stop_button->setEnabled(true);
//        this->ui->mysql_restart_button->setEnabled(false);
        this->ui->mysql_start_or_stop_button->setText("继续");
        break;
    case SERVICE_RUNNING:
        this->internal_mysql_status = MYSQL_RUNNING;
        this->ui->mysql_start_or_stop_button->setEnabled(true);
//        this->ui->mysql_restart_button->setEnabled(true);
        this->ui->mysql_start_or_stop_button->setText("关闭");
        break;
    case SERVICE_START_PENDING:
        this->internal_mysql_status = MYSQL_RUNNING;
        this->ui->mysql_start_or_stop_button->setEnabled(false);
//        this->ui->mysql_restart_button->setEnabled(false);
        break;
    case SERVICE_STOP_PENDING:
        this->internal_mysql_status = MYSQL_STOPPED;
        this->ui->mysql_start_or_stop_button->setEnabled(false);
//        this->ui->mysql_restart_button->setEnabled(false);
        break;
    case SERVICE_STOPPED:
        this->internal_mysql_status = MYSQL_STOPPED;
        this->ui->mysql_start_or_stop_button->setEnabled(true);
//        this->ui->mysql_restart_button->setEnabled(false);
        this->ui->mysql_start_or_stop_button->setText("开启");
        break;
    }
}

void MainWindow::on_refresh_clicked()
{
    this->refresh_status();
}

void MainWindow::on_apache_start_or_stop_button_clicked()
{
    switch (this->internal_http_status) {
    case APACHE_RUNNING:
        if (!ControlService(this->sc_httpd_service, SERVICE_CONTROL_STOP, &this->http_status)) {
            QMessageBox::critical(this, "系统错误", "无法关闭HTTP服务：" + QVariant((qulonglong)GetLastError()).toString());
            return;
        }
        this->internal_http_status = APACHE_STOPPED;
        break;
    case APACHE_STOPPED:
        if (!StartService(this->sc_httpd_service, 0, NULL)) {
            QMessageBox::warning(this, "系统警告", "HTTP无法启动：" + QVariant((qulonglong)GetLastError()).toString());
            return;
        }
        this->internal_http_status = APACHE_RUNNING;
        break;
    default:
        QMessageBox::warning(this, "系统警告", "未知HTTP状态");
        break;
    }
}

void MainWindow::on_mysql_start_or_stop_button_clicked()
{
    switch (this->internal_mysql_status) {
    case MYSQL_RUNNING:
        if (!ControlService(this->sc_mysqld_service, SERVICE_CONTROL_STOP, &this->mysql_status)) {
            QMessageBox::critical(this, "系统错误", "无法关闭MySQL服务：" + QVariant((qulonglong)GetLastError()).toString());
            return;
        }
        this->internal_mysql_status = MYSQL_STOPPED;
        break;
    case MYSQL_STOPPED:
    case MYSQL_PAUSED:
        if (!StartService(this->sc_mysqld_service, 0, NULL)) {
            QMessageBox::warning(this, "系统警告", "MySQL无法启动：" + QVariant((qulonglong)GetLastError()).toString());
            return;
        }
        this->internal_mysql_status = MYSQL_RUNNING;
        break;
    default:
        QMessageBox::warning(this, "系统警告", "未知MySQL状态");
        break;
    }
}

/**
 * 目前下面的功能还有问题，以后再慢慢研究和优化吧……
 */

//void MainWindow::on_apache_restart_button_clicked()
//{
//    this->ui->apache_restart_button->setEnabled(false);
//    if (!ControlService(this->sc_httpd_service, SERVICE_CONTROL_STOP, &this->http_status)) {
//        QMessageBox::critical(this, "系统错误", "无法关闭HTTP服务：" + QVariant((qulonglong)GetLastError()).toString());
//        return;
//    }
//    if (!StartService(this->sc_httpd_service, 0, NULL)) {
//        QMessageBox::warning(this, "系统警告", "HTTP无法启动：" + QVariant((qulonglong)GetLastError()).toString());
//        return;
//    }
//    this->refresh_status();
//}

//void MainWindow::on_mysql_restart_button_clicked()
//{
//    this->ui->mysql_restart_button->setEnabled(false);
//    if (!ControlService(this->sc_mysqld_service, SERVICE_CONTROL_STOP, &this->http_status)) {
//        QMessageBox::critical(this, "系统错误", "无法关闭MySQL服务：" + QVariant((qulonglong)GetLastError()).toString());
//        return;
//    }
//    if (!StartService(this->sc_mysqld_service, 0, NULL)) {
//        QMessageBox::warning(this, "系统警告", "MySQL无法启动：" + QVariant((qulonglong)GetLastError()).toString());
//        return;
//    }
//    this->refresh_status();
//}
